#!/usr/bin/env python3
import os
import subprocess
import sys
import threading
import shutil

def start_os_stat(time, file):
    command = "oc-ops os_stat -fg 2 -de %d >> %s \
    " % (int(time), file)
    #os.system(command)
    result = subprocess.run(command, shell=True, text=True, capture_output=True)

def thread(docker, command, time, file):
    #print("test: start", command)

    command1 = "docker exec -it  --privileged   %s yum install kernel-devel-6.6.47-12.tl4.x86_64 -y \
    " % (docker)
    result = subprocess.run(command1, shell=True, text=True, capture_output=True)
    command2 = "docker exec -it  --privileged   %s yum install opencloudos-tools -y \
    " % (docker)
    result = subprocess.run(command2, shell=True, text=True, capture_output=True)
    command3 = "docker exec -it  --privileged   %s  yum install kmod -y \
    " % (docker)
    result = subprocess.run(command3, shell=True, text=True, capture_output=True)

    command = "docker exec -it  --privileged   %s /data/vdbench50407a_v10/vdbench -f \
    /data/vdbench50407a_v10/examples/filesys/%s  -e %d -o test/ >> %s \
    " % (docker, command, int(time), file)
    #os.system(command)
    result = subprocess.run(command, shell=True, text=True, capture_output=True)


def do_the_test(benchmakr_name, sysctl_value, time):
    threads = []
    docker = ["parameter_test_001", "parameter_test_002", "parameter_test_003", "parameter_test_004", "parameter_test_005", \
              "parameter_test_006", "parameter_test_007", "parameter_test_008"]
    command = ["create_files", "delete_files", "random_read", "random_rw", "random_write", \
               "seq_read", "seq_rw", "seq_write"]

    src_dir = "./tmp_vdbench"
    dest_dir = "./history_vdbench"

    isExists=os.path.exists(src_dir)
    if not isExists:
        os.makedirs(src_dir)
    isExists=os.path.exists(dest_dir)
    if not isExists:
        os.makedirs(dest_dir)

    for i in range(8):
        benchmark_file = "./tmp_vdbench/%d.%s_%s" % ((i + 1), benchmakr_name, command[i])
        with open(benchmark_file, 'w+') as file:
            print("\nfile:"+ sysctl_value + "\n", file=file, end=' ', flush=True)
        t = threading.Thread(target=thread, args=(docker[i], command[i], time, benchmark_file))
        threads.append(t)
        t.start()

    benchmark_file1 = "./tmp_vdbench/%d.%s_%s_info_max" % ((i + 1), benchmakr_name, command[i])
    with open(benchmark_file1, 'w+') as file:
        print("\nfile:"+ sysctl_value + "\n", file=file, end=' ', flush=True)
    t = threading.Thread(target=start_os_stat, args=(time, benchmark_file1))
    threads.append(t)
    t.start()

    # 等待所有线程执行完毕
    for t in threads:
        t.join()

    for i in range(8):
        src_file = "./tmp_vdbench/%d.%s_%s" % ((i + 1), benchmakr_name, command[i])
        dest_file = "./history_vdbench/%d.%s_%s" % ((i + 1), benchmakr_name, command[i])
        shutil.move(src_file, dest_file)
    src_file = benchmark_file1
    file = open(src_file, 'r')
    data = file.read()
    for i in range(8):
        dest_file = "./history_vdbench/%d.%s_%s_info_max" % ((i + 1), benchmakr_name, command[i])
        dest = open(dest_file, 'w+')
        dest.write(data)
        dest.close()
    file.close()
    #print("All threads are finished.")
